<?php
namespace app\backend\controller;

use app\backend\model\Voice as VoiceModel;
use app\backend\model\Tab as TabModel;
use app\backend\model\Type as TypeModel;
use think\Db;
use think\Request;
use app\backend\org\Helper;
use app\backend\org\Image;

class Voices extends Base
{
    public $url_path = "/static/img/video/";

    //选择ftp的视频
    public function voices_path(){
        try {
            return "{$_SERVER['DOCUMENT_ROOT']}/static/img/video/";
        }catch (\Exception $e){
            echo '<pre>';
            print_r($e);
            exit;
        }
    }
    // 部门列表展示
    public function index()
    {
        $model = new VoiceModel;
        // 获取分页显示
        $result = $model->getAll()->paginate(5);

        foreach ($result as $k=>$v){
            if($v["type_id"]==3){
                $v["vioce_url"]  = substr($v['vioce_url'],strrpos("{$v['vioce_url']}","/")+1);
            }

        }
        $page = $result->render();
        $this->assign('page',$page);
        $this->assign('voice',$result);
        $this->assign('empty',"暂无数据");
        //$this->assign('act','index');//高亮js
        return $this->fetch();

    }

    //新建部门
    public function voices_create(){
        if (Request::instance()->isPost())
        {
            $model = new VoiceModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            if($post["type_id"]==3){
                $post["vioce_url"] = $this->url_path.$post["choose"];
            }
            //var_dump($post);exit;
            unset($post["choose"]);
            //调用图片上传助手类
            if(!(Image::image($post,VIDEO_PATH))){
                $this->error("图片上传失败");
            }

            unset($post["img_type"]);
            //var_dump($post);exit;
            // die();

            // 验证表单并插入数据库
            $result = $this->validate($post,'Voice.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');
            //$model->insert($post);

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('voices/index');
            }
        }
        //ftp视频
        $file = scandir($this->voices_path());
       // var_dump($file);exit;
        unset($file[0],$file[1]);
        $this->assign('choose',$file);
        //tab_id领星新闻
        $tab_id = TabModel::where(["tab_name_cn"=>'领星新闻',"delete_time"=>["exp","is null"]])->value("tab_id");
        $this->assign('tab_id',$tab_id);

        //视频type
        $type = TypeModel::where(["type_id"=>["exp","in(3,4)"],"delete_time"=>["exp","is null"]])->select();
        //var_dump($type);exit;
        $this->assign('type',$type);

        //查询某个分类的直接子类id和名字
        $this->assign('act','index');
        return $this->fetch();
    }

    //部门修改
    public function voices_update($id)
    {
        if (Request::instance()->isPost())
        {
            $model = new VoiceModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            if($post["type_id"]==3){
                $post["vioce_url"] = $this->url_path.$post["choose"];
            }
            unset($post["choose"]);
            //var_dump($post);exit;
            //调用图片上传助手类
            if(!(Image::image($post,VIDEO_PATH))){
                $this->error("图片上传失败");
            }
            //var_dump($post);exit;
            unset($post["img_type"]);
            //验证表单并插入数据库
            $result = $this->validate($post,'Voice.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['vioce_id'=>$id]);
            //echo $result;exit;
            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->redirect('voices/index');
            }
        }

        //ftp视频
        $file = scandir($this->voices_path());
        // var_dump($file);exit;
        unset($file[0],$file[1]);
        $this->assign('choose',$file);

        //根据主键插记录
        $record = VoiceModel::get($id);
        //var_dump($record);exit;
        $this->assign('id',$id);
        $this->assign('voices', $record);
        //print_r(substr($record['vioce_url'],strrpos("{$record['vioce_url']}","/")+1));exit;
        $this->assign('vio_select', substr($record['vioce_url'],strrpos("{$record['vioce_url']}","/")+1));
        $this->assign('type_id',$record["type_id"]);

        return $this->fetch();
    }

    //删除部门  软删除，记录删除时间
    public function voices_delete($id)
    {
        VoiceModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect('voices/index');
        return $this->fetch();
    }



}